﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Evaluation3;

public partial class uicontrol_dnTaoMoiSP : System.Web.UI.UserControl
{
    sanphamService objSP = new sanphamService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ResetForm();
        }
    }

    #region grvSP_RowDataBound()
    //Giấu đi các cột ko mong muốn hiển thị
    protected void grvSP_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Sản phẩm            
            e.Row.Cells[0].Visible = false;
        }
        //Thêm Comfirm Message Box vào cho nút xóa
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton ibDel = (ImageButton)e.Row.FindControl("update");
            ibDel.Attributes.Add("onclick", "return confirm('Bạn muốn xóa sản phẩm " + DataBinder.Eval(e.Row.DataItem, "TenSanPham") + " ra khỏi dữ liệu ?')");
        }
    } 
    #endregion

    #region Tạo mới sản phẩm
    protected void btnNew_Click(object sender, EventArgs e)
    {
        try
        {
            objSP.TenSanPham = txtTenSP.Text.Trim();
            objSP.MaDoanhNghiep = int.Parse(Session["CompanyID"].ToString());
            objSP.KyThuatSanXuat = txtCongNghe.Text.Trim();
            objSP.ThiTruong = txtThiTruong.Text.Trim();
            objSP.TieuChuanSanPham = txtTieuChuanSP.Text.Trim();
            objSP.NamSanXuat = txtNamSX.Text.Trim();
            objSP.MaHoiDong = int.Parse(ddlHD.SelectedValue);
            objSP.SP_New();
            Bind_Grv();
            lblResult.Text = "Đã thêm mới sản phẩm cho doanh nghiệp.";
        }
        catch (Exception ex)
        {
            throw ex;
        }
    } 
    #endregion

    #region grvSP_RowCommand()
    public void Bind_Grv()
    {
        dsSP.DataBind();
        grvSP.DataBind();
    }
    protected void grvSP_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            //dsSP.UpdateCommand = "UPDATE SanPham SET Status = 0 WHERE (MaSanPham = @MaSanPham)";
            dsSP.UpdateParameters.Add("MaSanPham", TypeCode.Int32, e.CommandArgument.ToString());
            dsSP.Update();
            Bind_Grv();           
        }
        if (e.CommandName == "GiaCong")
        {            
            Session["spID"] = e.CommandArgument;
            Response.Redirect("adminRender.aspx?mod=buocGC");
        }
        if (e.CommandName == "Sua")
        {
            btnNew.Visible = false;
            btnUpdate.Visible = true;
            lblResult.Text = "";
            Session["spID"] = e.CommandArgument;

            Load_Data();
        }
    } 
    #endregion

    #region btnCancel_Click()
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ResetForm();
    }
    public void ResetForm()
    {
        txtCongNghe.Text = "";
        txtNamSX.Text = DateTime.Now.Year.ToString();
        txtTenSP.Text = "";
        txtThiTruong.Text = "";
        txtTieuChuanSP.Text = "";
        ddlHD.SelectedIndex = 0;
        
        lblResult.Text = "";
        btnUpdate.Visible = false;
        btnNew.Visible = true;
        Bind_Grv();
    }
    #endregion    

    #region grvSP_PageIndexChanging()
    protected void grvSP_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvSP.PageIndex = e.NewPageIndex;
        Bind_Grv();
    } 
    #endregion
    
    #region btnUpdate_Click()
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        lblResult.Text = "Đã cập nhật.";
        dsSP_Info.Update();
        Bind_Grv();
    }
    #endregion

    public void Load_Data()
    {
        DataTable dt = ((DataView)dsSP_Info.Select(DataSourceSelectArguments.Empty)).Table;
        txtTenSP.Text = dt.Rows[0]["TenSanPham"].ToString();
        ddlHD.SelectedValue = dt.Rows[0]["MaHoiDong"].ToString();
        txtCongNghe.Text = dt.Rows[0]["KyThuatSanXuat"].ToString();
        txtThiTruong.Text = dt.Rows[0]["ThiTruong"].ToString();
        txtTieuChuanSP.Text = dt.Rows[0]["TieuChuanSanPham"].ToString();
        txtNamSX.Text = dt.Rows[0]["NamSanXuat"].ToString();

        dt.Dispose();
    }
}
